<template>
  <div id="modify-gateway-env">
    <p class="title-pad20">服务环境基本信息</p>
    <el-form :model="gwInfoForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-row>
        <el-col :push=2 :span=10>
          <el-form-item label="网关编号:" prop="gwEnvCode">
            <el-input v-model="gwInfoForm.gwEnvCode"></el-input>
          </el-form-item>
        </el-col>
        <el-col :push=2 :span=10>
          <el-form-item label="网关名称:" prop="gwEnvName">
            <el-input v-model="gwInfoForm.gwEnvName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :push=2 :span=10>
          <el-form-item label="网关外部地址:" prop="gwEnvUrl">
            <el-input v-model="gwInfoForm.gwEnvUrl"></el-input>
          </el-form-item>
        </el-col>
        <el-col :push=2 :span=10>
          <el-form-item label="网关部署地址:" prop="gwEnvAddress">
            <el-input v-model="gwInfoForm.gwEnvAddress"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :push=2 :span=10>
          <el-form-item label="网关服务状态:">
            <el-select v-model="gwInfoForm.gwEnvStatus" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :push=2 :span=10>
          <el-form-item label="网关环境描述:" prop="gwEnvDesc">
            <el-input v-model="gwInfoForm.gwEnvDesc"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :push=2 :span=10>
          <el-form-item label="是否鉴权:" prop="gwGrantFlag">
            <el-switch v-model="gwInfoForm.gwGrantFlag"></el-switch>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col>
          <el-button class="pd-w30" type="primary" size="mini" @click="reset">重置</el-button>
          <el-button class="pd-w30" type="primary" size="mini" @click="modifyGwEnvFn">提交</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import api from '../../../../../api/url'
import ajax from '../../../../../api/ajax/ajax'
export default {
  data () {
    return {
      modifyCurrentData: {},
      modifyCurrentData2: {},
      options: [{
        value: '01',
        label: '停止服务'
      }, {
        value: '00',
        label: '开放服务'
      }],
      value: '',
      gwInfoForm: {
        gwEnvCode: '',
        gwEnvName: '',
        gwEnvUrl: '',
        gwEnvAddress: '',
        gwEnvStatus: '',
        gwEnvDesc: '',
        gwGrantFlag: false
      },
      currentFormData: {},
      rules: {
        gwEnvCode: [
          { required: true, message: '请输入网关编号', trigger: 'blur' }
        ],
        gwEnvName: [
          { required: true, message: '请输入网关名称', trigger: 'blur' }
        ],
        gwEnvUrl: [
          { required: true, message: '请输入网关外不地址', trigger: 'blur' }
        ],
        gwEnvAddress: [
          { required: true, message: '请输入网关部署地址', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // tab切换清空表单&&验证&&重置
    clearForm () {
      this.$refs.ruleForm.clearValidate()
    },
    reset () {
      this.gwInfoForm = JSON.parse(JSON.stringify(this.currentFormData))
      this.$refs.ruleForm.clearValidate()
    },
    modifyGwEnvFn () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.gwInfoForm.gwGrantFlag = this.gwInfoForm.gwGrantFlag ? '1' : '0'
          ajax.post(api.MODIFY_GW, this.gwInfoForm).then((res) => {
            this.$message({
              type: 'success',
              message: '修改成功'
            })
            if (this.gwInfoForm.gwGrantFlag === '1') {
              this.gwInfoForm.gwGrantFlag = true
            } else {
              this.gwInfoForm.gwGrantFlag = false
            }
            Object.keys(this.modifyCurrentData).forEach(key => {
              this.modifyCurrentData2[key] = this.modifyCurrentData[key]
            })
            this.clearForm()
          }).catch(err => {
            console.info(err)
          })
        }
      })
    }
  },
    // 修改网关环境
  mounted () {
    this.$on('gwEnvmodify', ({selectData}) => {
      this.modifyCurrentData2 = selectData
      this.modifyCurrentData = JSON.parse(JSON.stringify(selectData))
      this.gwInfoForm = this.modifyCurrentData
      this.currentFormData = JSON.parse(JSON.stringify(this.gwInfoForm))
    })
  }
}
</script>
<style lang="stylus" rel="stylesheet/stylus">
  #modify-gateway-env
    background #ffffff
    padding-bottom 10px
    .title-pad20
      height: 40px
      line-height 40px
      padding-left 20px
    .pd-w30
      padding 7px 30px
    .el-form-item__label
      width 120px
    .el-form-item
      margin-bottom 22px
    .el-select
    .el-input
      width 100%
    .el-col-10
      // margin-top 10px
    .el-col-24
      text-align center
      margin-bottom 20px
</style>
